﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HousingDistribution
{
    partial class FormPersonDetailsTab
    {
        //上一个按钮。
        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            if (this.curIndex > 0)
                this.curIndex--;

            if (this.curIndex > 0)
            {
                buttonPrevious.Enabled = true;
                buttonNext.Enabled = true;
            }
            else
            {
                buttonPrevious.Enabled = false;
                buttonNext.Enabled = true;
            }

            DisplayInfo(this.curIndex);

            RefreshHousingDisState(this.curPerInfo);
        }

        //下一个按钮。
        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (this.curIndex < personCount - 1)
                this.curIndex++;

            if (this.curIndex < personCount - 1)
            {
                buttonPrevious.Enabled = true;
                buttonNext.Enabled = true;
            }
            else
            {
                buttonNext.Enabled = false;
                buttonPrevious.Enabled = true;
            }

            DisplayInfo(this.curIndex);

            RefreshHousingDisState(this.curPerInfo);
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            StringBuilder sql = new StringBuilder("delete person, personsortinfo from person left join personsortinfo on person.idperson = personsortinfo.idperson where person.idperson = '");
            sql.Append(this.curPerInfo.idperson.ToString());
            sql.Append("'");

            try
            {
                DBProc.Instance().Excute(sql.ToString());

                //数据库中总人数
                personCount = GetPersonCount();

                if (personCount > 0)
                {
                    if (this.curIndex == personCount)
                        this.curIndex--;

                    //删除数据造成视图与personTable不一致，重新读取
                    this.personTable = null;
                    DisplayInfo(this.curIndex);
                }
                else
                {
                    //将当前信息清空
                    this.curPerInfo.idperson = 0;

                    //清空界面
                    ResetContrlsBesidesTab();

                    string type = tabControl.SelectedTab.Text;

                    if (type == "军人干部")
                        ResetArmmanInfoTab();
                    else if (type == "职员干部")
                        ResetStaffInfoTab();
                    else if (type == "工人")
                        ResetWorkerInfoTab();
                    else if (type == "高级士官")
                        ResetNCOInfoTab();
                    else
                    {
                    }

                    this.buttonPrevious.Enabled = false;
                    this.buttonNext.Enabled = false;
                    this.buttonDelete.Enabled = false;
                }
                
                RefreshHousingDisState(this.curPerInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            PersonTypeEnum type = DataDictionary.Instance().GetPersonType(tabControl.SelectedTab.Text);

            string sql = null;

            try
            {
                if (type == PersonTypeEnum.ArmMan)
                {
                    GetValueFromArmman(ref this.curPerInfo);
                }
                else if (type == PersonTypeEnum.Staff)
                {
                    GetValueFromStaff(ref this.curPerInfo);
                }
                else if (type == PersonTypeEnum.Worker)
                {
                    GetValueFromWorker(ref this.curPerInfo);
                }
                else if (type == PersonTypeEnum.NCO)
                {
                    GetValueFromNCO(ref this.curPerInfo);
                }
                else
                {
                    return;
                }

                this.curPerInfo.type = type;
                sql = this.sqlCreator.InsertSql(this.curPerInfo);

                //name == null 表明数据转换时有问题，则不处理
                if (this.curPerInfo.name == null)
                {
                    MessageBox.Show("创建人员必需要输入姓名");
                    return;
                }

                InfoSearcher infoSearcher = new InfoSearcher();
                //首先检查数据库中是否存在对应的记录
                if (infoSearcher.PersonExisted(this.curPerInfo))
                {
                    MessageBox.Show("数据库中已存在高度匹配的人员，请确认信息！");
                    return;
                }

                //名字首字母
                this.curPerInfo.nameInitial = Spell.MakeSpellCode(this.curPerInfo.name, SpellOptions.FirstLetterOnly);

                string insertPersonInfoSql = this.sqlCreator.InsertSql(this.curPerInfo);

                this.curPerInfo.idperson = DBProc.Instance().ExcuteInsertSql(insertPersonInfoSql);

                if (this.curPerInfo.idperson > 0)
                {
                    //最后，生成该人员的排序信息并存入数据库
                    PersonSortInfoProc sortInfoProc = new PersonSortInfoProc();
                    sortInfoProc.Proc(this.curPerInfo);

                    //人员总数增加1
                    personCount++;
                }
                else
                {
                    MessageBox.Show("数据入库出现错误：无法获取人员id");
                }

                RefreshHousingDisState(this.curPerInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.personTable = null;
            this.curIndex = 0;
            DisplayInfo(this.curIndex);
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            PersonTypeEnum type = DataDictionary.Instance().GetPersonType(tabControl.SelectedTab.Text);
            this.curPerInfo.type = type;

            string sql = null;

            try
            {
                PersonSortInfoProc sortInfoProc = new PersonSortInfoProc();

                if (type == PersonTypeEnum.ArmMan)
                {
                    GetValueFromArmman(ref this.curPerInfo);
                    sql = this.sqlCreator.UpdateExceptHousing(this.curPerInfo);
                    //生成该人员的排序信息并存入数据库
                    sortInfoProc.Proc(this.curPerInfo);
                    RefreshHousingDisState(this.curPerInfo);

                }
                else if (type == PersonTypeEnum.Staff)
                {
                    GetValueFromStaff(ref this.curPerInfo);
                    sql = this.sqlCreator.UpdateExceptHousing(this.curPerInfo);

                    sortInfoProc.Proc(this.curPerInfo);
                    RefreshHousingDisState(this.curPerInfo);
                }
                else if (type == PersonTypeEnum.Worker)
                {
                    GetValueFromWorker(ref this.curPerInfo);
                    sql = this.sqlCreator.UpdateExceptHousing(this.curPerInfo);

                    sortInfoProc.Proc(this.curPerInfo);
                    RefreshHousingDisState(this.curPerInfo);
                }
                else if (type == PersonTypeEnum.NCO)
                {
                    GetValueFromNCO(ref this.curPerInfo);
                    sql = this.sqlCreator.UpdateExceptHousing(this.curPerInfo);

                    sortInfoProc.Proc(this.curPerInfo);
                    RefreshHousingDisState(this.curPerInfo);
                }
                else
                {
                }

                DBProc.Instance().Excute(sql);

                UpdateBothEmpValue();

                //每次修改数据并不是重新读取数据库，因此需要更新内存中的DataTable保持一致
                int index = this.curIndex % MAX_COUNT;
                UpdateDataTable(ref this.personTable, index, this.curPerInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateBothEmpValue()
        {
            int bothEmpFlag = this.checkBoxBothEmp.Checked ? 1 : 0;

            string sql = this.sqlCreator.UpdateOnlyForEmployee(curPerInfo, bothEmpFlag);
            DBProc.Instance().Excute(sql);
        }

        //当修改界面的值之后，需要更新当前内存中的personTable
        private void UpdateDataTable(ref DataTable dt, int rowIndex, PersonInfo personInfo)
        {
            DataRow dr = dt.Rows[rowIndex];

            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME)] = personInfo.name;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE)] = DataDictionary.Instance().GetPersonType(personInfo.type);
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SEX)] = personInfo.sex;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT)] = personInfo.unit;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY)] = personInfo.adminDuty;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY_TIME)] = personInfo.adminDutyTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK)] = personInfo.techRank;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK_TIME)] = personInfo.techRankTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE)] = personInfo.techTitle;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE_TIME)] = personInfo.techTitleTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.CUR_RANK_BEFORE_TIME)] = personInfo.curRankBeforeTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME)] = personInfo.workTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SCHOOL_TIME)] = personInfo.schoolTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SIGNUP_TIME)] = personInfo.signupTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SERVICE_LENGTH)] = personInfo.serviceLength;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.RETIREMENT_TIME)] = personInfo.retirementTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_NAME)] = personInfo.mateInfo.name;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_RANK)] = personInfo.mateInfo.rank;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_UNIT)] = personInfo.mateInfo.unit;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.REMARKS)] = personInfo.remarks;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_ELIGIBLE)] = personInfo.housingEligible;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.BOTH_EMPLOYEE)] = personInfo.bothEmployee;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.EVALUATION)] = personInfo.evaluation;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSE_ADDR)] = personInfo.curHousingAddr.addr;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_INFO)] = this.sqlCreator.GetHousingInfoString(personInfo.housingInfo);
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.FORMER_HOUSING_ID)] = this.sqlCreator.GetFormerHousingIdString(personInfo.formerHousingId);
            
            int bothEmpFlag = this.checkBoxBothEmp.Checked ? 1 : 0;
            dr[dt.Columns.Count - 1] = bothEmpFlag;
        }

        //可分房
        private void checkBoxHousingEligible_Click(object sender, EventArgs e)
        {
            if (this.checkBoxHousingEligible.Checked)
            {
                this.curPerInfo.housingEligible = 1;
            }
            else
            {
                this.curPerInfo.housingEligible = 0;
            }
        }

        //*************人员信息界面相关***********************//
        private void DisplayInfo(int curIndex)
        {
            if (curIndex < 0 || curIndex >= personCount)
                return;

            try
            {
                int curOffset = curIndex / MAX_COUNT;

                //personTable 无效，需重新读取
                if (offset != curOffset || this.personTable == null)
                {
                    ResetPersonDisplayState();

                    offset = curOffset;
                    //读取对应区间的数据
                    this.personTable = ReadPersonData(offset * MAX_COUNT);
                }

                if (this.personTable.Rows.Count == 0)
                    return;

                int index = curIndex % MAX_COUNT;

                this.curPerInfo = this.parser.ParsePerson(this.personTable.Rows[index].ItemArray);
                
                DisplayPersonInfo(this.curPerInfo);

                //显示当前住房信息
                DisplayCurHousing(this.curPerInfo.housingInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DisplayPersonInfo(PersonInfo personInfo)
        {
            if (personInfo.type == PersonTypeEnum.ArmMan)
            {
                ArmmanInfo(personInfo);
                //控制相应的tab
                this.tabControl.SelectTab(this.tabArmMan);

                //清空其他tab的无效信息
                ResetStaffInfoTab();
                ResetNCOInfoTab();
                ResetWorkerInfoTab();
            }
            else if (personInfo.type == PersonTypeEnum.Staff)
            {
                StaffInfo(personInfo);
                this.tabControl.SelectTab(this.tabStaff);

                ResetArmmanInfoTab();
                ResetNCOInfoTab();
                ResetWorkerInfoTab();
            }
            else if (personInfo.type == PersonTypeEnum.Worker)
            {
                WorkerInfo(personInfo);
                this.tabControl.SelectTab(this.tabWorker);

                ResetArmmanInfoTab();
                ResetStaffInfoTab();
                ResetNCOInfoTab();
            }
            else if (personInfo.type == PersonTypeEnum.NCO)
            {
                NCOInfo(personInfo);
                this.tabControl.SelectTab(this.tabNCO);

                ResetArmmanInfoTab();
                ResetStaffInfoTab();
                ResetWorkerInfoTab();
            }
            else
            {
            }
        }
        
        //显示当前住房信息（this.dataGridViewCurHousing）
        private void DisplayCurHousing(List<HousingOccupancyInfo> housingInfo)
        {
            //idhousing > 0 表明有院内住房
            if (housingInfo != null && housingInfo.Count > 0)
            {
                try
                {
                    StringBuilder sql = new StringBuilder("select idhousing , housingType, housingFullAddr, housingUse, housingProperty from housing where ");
                    sql.Append(this.sqlCreator.GetHousingInfoQueryString(housingInfo));

                    DataTable dt = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

                    this.dataGridViewCurHousing.DataSource = dt;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            else
                this.dataGridViewCurHousing.DataSource = null;
        }

        //格式化显示时间，无效时间不显示，有效时间为YYYY/MM/DD
        private string DateTimeDisplayFormat(DateTime date)
        {
            if (date.CompareTo(DateProc.InvalidDate()) == 0)
                return null;
            else
                return date.ToShortDateString();
        }

        //界面共享区域信息显示
        void PersonSharingInfo(PersonInfo personInfo)
        {
            textBoxMateName.Text = personInfo.mateInfo.name;
            textBoxMateRank.Text = personInfo.mateInfo.rank;
            textBoxMateUnit.Text = personInfo.mateInfo.unit;
            textBoxCurAddr.Text = personInfo.curHousingAddr.addr;
            textBoxRemarks.Text = personInfo.remarks;
            textBoxFormerHousingInfo.Text = this.sqlCreator.GetFormerHousingIdString(personInfo.formerHousingId);
        }
        //界面共享区域信息获取
        void GetPersonSharingInfo(ref PersonInfo personInfo)
        {
            personInfo.mateInfo.name = textBoxMateName.Text;
            personInfo.mateInfo.rank = textBoxMateRank.Text;
            personInfo.mateInfo.unit = textBoxMateUnit.Text;
            personInfo.remarks = textBoxRemarks.Text;
            personInfo.curHousingAddr.addr = textBoxCurAddr.Text;
        }

        //军人干部
        private void ArmmanInfo(PersonInfo personInfo)
        {
            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            textBoxArmManName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnArmManMale.Checked = true;
            else
                radioBtnArmManFemale.Checked = true;

            comboBoxArmManUnit.Text = personInfo.unit;
            textBoxArmManAdminDuty.Text = personInfo.adminDuty;
            textBoxArmManAdminDutyTime.Text = DateTimeDisplayFormat(personInfo.adminDutyTime);
            comboBoxArmManTechRank.Text = personInfo.techRank;
            textBoxArmManTechRankTime.Text = DateTimeDisplayFormat(personInfo.techRankTime);
            comboBoxArmManTechTitle.Text = personInfo.techTitle;
            textBoxArmManTechTitleTime.Text = DateTimeDisplayFormat(personInfo.techTitleTime);
            textBoxArmManCurRankBeforeTime.Text = DateTimeDisplayFormat(personInfo.curRankBeforeTime);
            textBoxArmManWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxArmManSchoolTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxArmManSignupTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            
            PersonSharingInfo(personInfo);
        }

        private void ResetArmmanInfoTab()
        {
            textBoxArmManName.Text = null;
            radioBtnArmManMale.Checked = false;
            radioBtnArmManFemale.Checked = false;

            comboBoxArmManUnit.Text = null;
            textBoxArmManAdminDuty.Text = null;
            textBoxArmManAdminDutyTime.Text = null;
            comboBoxArmManTechRank.Text = null;
            textBoxArmManTechRankTime.Text = null;
            comboBoxArmManTechTitle.Text = null;
            textBoxArmManTechTitleTime.Text = null;
            textBoxArmManCurRankBeforeTime.Text = null;
            textBoxArmManWorkTime.Text = null;
            textBoxArmManSchoolTime.Text = null;
            textBoxArmManSignupTime.Text = null;
        }
        private void GetValueFromArmman(ref PersonInfo personInfo)
        {
            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;
            else
                personInfo.housingEligible = 0;

            personInfo.name = textBoxArmManName.Text;

            if (radioBtnArmManMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxArmManUnit.Text;
            personInfo.adminDuty = textBoxArmManAdminDuty.Text;
            personInfo.adminDutyTime = this.dateProc.Parse(textBoxArmManAdminDutyTime.Text);
            personInfo.techRank = comboBoxArmManTechRank.Text;
            personInfo.techRankTime = this.dateProc.Parse(textBoxArmManTechRankTime.Text);
            personInfo.techTitle = comboBoxArmManTechTitle.Text;
            personInfo.techTitleTime = this.dateProc.Parse(textBoxArmManTechTitleTime.Text);
            personInfo.curRankBeforeTime = this.dateProc.Parse(textBoxArmManCurRankBeforeTime.Text);
            personInfo.workTime = this.dateProc.Parse(textBoxArmManWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxArmManSchoolTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxArmManSignupTime.Text);

            GetPersonSharingInfo(ref personInfo);
        }

        //职员干部
        private void StaffInfo(PersonInfo personInfo)
        {
            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            textBoxStaffName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnStaffMale.Checked = true;
            else
                radioBtnStaffFemale.Checked = true;

            comboBoxStaffUnit.Text = personInfo.unit;
            textBoxStaffAdminDuty.Text = personInfo.adminDuty;
            textBoxStaffAdminDutyTime.Text = DateTimeDisplayFormat(personInfo.adminDutyTime);
            comboBoxStaffTechTitle.Text = personInfo.techTitle;
            textBoxStaffTechTitleTime.Text = DateTimeDisplayFormat(personInfo.techTitleTime);
            textBoxStaffCurRankBeforeTime.Text = DateTimeDisplayFormat(personInfo.curRankBeforeTime);
            textBoxStaffWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxStaffSchoolTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxStaffSignupTime.Text = DateTimeDisplayFormat(personInfo.signupTime);

            PersonSharingInfo(personInfo);
        }
        private void ResetStaffInfoTab()
        {
            textBoxStaffName.Text = null;

            radioBtnStaffMale.Checked = false;
            radioBtnStaffFemale.Checked = false;

            comboBoxStaffUnit.Text = null;
            textBoxStaffAdminDuty.Text = null;
            textBoxStaffAdminDutyTime.Text = null;
            comboBoxStaffTechTitle.Text = null;
            textBoxStaffTechTitleTime.Text = null;
            textBoxStaffCurRankBeforeTime.Text = null;
            textBoxStaffWorkTime.Text = null;
            textBoxStaffSchoolTime.Text = null;
            textBoxStaffSignupTime.Text = null;
        }

        private void GetValueFromStaff(ref PersonInfo personInfo)
        {
            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;

            personInfo.name = textBoxStaffName.Text;

            if (radioBtnStaffMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxStaffUnit.Text;
            personInfo.adminDuty = textBoxStaffAdminDuty.Text;
            personInfo.adminDutyTime = this.dateProc.Parse(textBoxStaffAdminDutyTime.Text);
            personInfo.techTitle = comboBoxStaffTechTitle.Text;
            personInfo.techTitleTime = this.dateProc.Parse(textBoxStaffTechTitleTime.Text);
            personInfo.curRankBeforeTime = this.dateProc.Parse(textBoxStaffCurRankBeforeTime.Text);
            personInfo.workTime = this.dateProc.Parse(textBoxStaffWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxStaffSchoolTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxStaffSignupTime.Text);

            GetPersonSharingInfo(ref personInfo);
        }

        //高级士官
        private void NCOInfo(PersonInfo personInfo)
        {
            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            textBoxNCOName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnNCOMale.Checked = true;
            else
                radioBtnNCOFemale.Checked = true;

            comboBoxNCOUnit.Text = personInfo.unit;
            textBoNCOTechTitle.Text = personInfo.techTitle;
            textBoxNCOTechTitleTime.Text = DateTimeDisplayFormat(personInfo.techTitleTime);
            textBoxNCOWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxNCOSignupTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);

            PersonSharingInfo(personInfo);
        }
        private void ResetNCOInfoTab()
        {
            textBoxNCOName.Text = null;
            radioBtnNCOMale.Checked = false;
            radioBtnNCOFemale.Checked = false;

            comboBoxNCOUnit.Text = null;
            textBoNCOTechTitle.Text = null;
            textBoxNCOTechTitleTime.Text = null;
            textBoxNCOWorkTime.Text = null;
            textBoxNCOSignupTime.Text = null;
        }
        private void GetValueFromNCO(ref PersonInfo personInfo)
        {
            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;

            personInfo.name = textBoxNCOName.Text;

            if (radioBtnNCOMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxNCOUnit.Text;
            personInfo.techTitle = textBoNCOTechTitle.Text;
            personInfo.techTitleTime = this.dateProc.Parse(textBoxNCOTechTitleTime.Text);
            personInfo.workTime = this.dateProc.Parse(textBoxNCOWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxNCOSignupTime.Text);

            GetPersonSharingInfo(ref personInfo);
        }
        //工人
        private void WorkerInfo(PersonInfo personInfo)
        {
            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            textBoxWorkerName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnWorkerMale.Checked = true;
            else
                radioBtnWorkerFemale.Checked = true;

            comboBoxWorkerUnit.Text = personInfo.unit;
            comboBoxWorkerTechTitle.Text = personInfo.techTitle;
            textBoxWorkerWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxWorkerSchoolTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxWorkerSignupTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxWorkerRetirementTime.Text = DateTimeDisplayFormat(personInfo.retirementTime);
            textBoxWorkerServiceLength.Text = personInfo.serviceLength.ToString();

            PersonSharingInfo(personInfo);
        }
        private void ResetWorkerInfoTab()
        {
            textBoxWorkerName.Text = null;
            radioBtnWorkerMale.Checked = false;
            radioBtnWorkerFemale.Checked = false;

            comboBoxWorkerUnit.Text = null;
            comboBoxWorkerTechTitle.Text = null;
            textBoxWorkerWorkTime.Text = null;
            textBoxWorkerSchoolTime.Text = null;
            textBoxWorkerSignupTime.Text = null;
            textBoxWorkerRetirementTime.Text = null;
            textBoxWorkerServiceLength.Text = null;
        }
        private void GetValueFromWorker(ref PersonInfo personInfo)
        {
            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;

            personInfo.name = textBoxArmManName.Text;

            if (radioBtnWorkerMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxWorkerUnit.Text;
            personInfo.techTitle = comboBoxWorkerTechTitle.Text;
            personInfo.workTime = this.dateProc.Parse(textBoxWorkerWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxWorkerSchoolTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxWorkerSignupTime.Text);
            personInfo.retirementTime = this.dateProc.Parse(textBoxWorkerRetirementTime.Text);
            personInfo.serviceLength = Convert.ToInt32(textBoxWorkerServiceLength.Text);

            GetPersonSharingInfo(ref personInfo);
        }

        void ResetContrlsBesidesTab()
        {
            this.textBoxMateName.Text = null;
            this.textBoxMateRank.Text = null;
            this.textBoxMateUnit.Text = null;
            this.checkBoxBothEmp.Checked = false;
            this.checkBoxHousingEligible.Checked = false;
            this.textBoxRemarks.Text = null;

            this.dataGridViewCurHousing.DataSource = null;
        }

        //*************数据查询*******************//
        private int GetPersonCount()
        {
            StringBuilder sql = new StringBuilder("select count(*) from person where 1=1 ");
            sql.Append(GetNameInitialQueryString());

            try
            {
                DataTable dt = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

                return Convert.ToInt32(dt.Rows[0][0]);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        //当前数据查询条件下的数据总量
        private int personCount = 0;
        //存放查询结果的表
        private DataTable personTable = null;
        //当前人员信息
        PersonInfo curPerInfo;
        //表最大的容量
        private int MAX_COUNT = 100;

        //按 MAX_COUNT 为单位的偏移量
        private int offset = 0;
        //当前的数据的index [0, personCount-1]
        //curIndex / MAX_COUNT = offset
        private int curIndex = 0;

        private static string PERSON_QUERY_SQL_HEADER = "select person.*,  personsortinfo.sortMateIsEmployee from person left join personsortinfo on personsortinfo.idperson = person.idperson where 1=1 ";
        ObjectParser parser = new PersonParserFromMySql();
        private DataTable ReadPersonData(int offset)
        {
            StringBuilder sql = new StringBuilder(PERSON_QUERY_SQL_HEADER);
            sql.Append(GetNameInitialQueryString());
            sql.Append("order by idperson desc limit ");
            sql.Append(offset.ToString());
            sql.Append(", ");
            sql.Append(MAX_COUNT.ToString());

            return DBProc.Instance().ReadData(sql.ToString()).Tables[0];
        }

        private void ResetPersonDisplayState()
        {
            this.personCount = GetPersonCount();
            this.curIndex = 0;
            this.offset = 0;
        }

        DateProc dateProc = new DateProc("YY-MM-DD");
        //*****************************************************************************
        //界面相关代码
        private void InitDGVCurHousing()
        {
            this.dataGridViewCurHousing.AutoGenerateColumns = false;

            this.dataGridViewCurHousing.Columns.Add(DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID), DataDictionary.Instance().HousingTableFieldTitle(HousingColConsts.ID));
            this.dataGridViewCurHousing.Columns.Add(DataDictionary.Instance().HousingTableFieldName(HousingColConsts.TYPE), DataDictionary.Instance().HousingTableFieldTitle(HousingColConsts.TYPE));
            this.dataGridViewCurHousing.Columns.Add(DataDictionary.Instance().HousingTableFieldName(HousingColConsts.FULL_ADDR), DataDictionary.Instance().HousingTableFieldTitle(HousingColConsts.FULL_ADDR));
            this.dataGridViewCurHousing.Columns.Add(DataDictionary.Instance().HousingTableFieldName(HousingColConsts.HOUSING_USE), DataDictionary.Instance().HousingTableFieldTitle(HousingColConsts.HOUSING_USE));
            this.dataGridViewCurHousing.Columns.Add(DataDictionary.Instance().HousingTableFieldName(HousingColConsts.HOUSING_PROPERTY), DataDictionary.Instance().HousingTableFieldTitle(HousingColConsts.HOUSING_PROPERTY));
            
            //把idhousing 一栏隐藏
            dataGridViewCurHousing.Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID)].Visible = false;

            //bind data
            this.dataGridViewCurHousing.Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID)].DataPropertyName = DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID);
            this.dataGridViewCurHousing.Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.TYPE)].DataPropertyName = DataDictionary.Instance().HousingTableFieldName(HousingColConsts.TYPE);
            this.dataGridViewCurHousing.Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.FULL_ADDR)].DataPropertyName = DataDictionary.Instance().HousingTableFieldName(HousingColConsts.FULL_ADDR);
            this.dataGridViewCurHousing.Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.HOUSING_USE)].DataPropertyName = DataDictionary.Instance().HousingTableFieldName(HousingColConsts.HOUSING_USE);
            this.dataGridViewCurHousing.Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.HOUSING_PROPERTY)].DataPropertyName = DataDictionary.Instance().HousingTableFieldName(HousingColConsts.HOUSING_PROPERTY);
        }

        private void CreateUnitComboxItem(ComboBox cb)
        {
            string[] unit = DataDictionary.Instance().Unit();

            for (int i = 0; i < unit.Length; i++)
            {
                cb.Items.Add(unit[i]);
            }
        }

        private void CreateTechRankComboxItem(ComboBox cb)
        {
            string[] techRank = DataDictionary.Instance().ArmmanTechRank();

            for (int i = 0; i < techRank.Length; i++)
            {
                cb.Items.Add(techRank[i]);
            }
        }

        private void CreateTechTitleComboxItem(ComboBox cb)
        {
            int i = 0;

            //高级职称
            string[] highTechTitle = DataDictionary.Instance().ArmmanHighTechTitle();

            for (; i < highTechTitle.Length; i++)
            {
                cb.Items.Add(highTechTitle[i]);
            }
            //非高级职称
            string[] midTechTitle = DataDictionary.Instance().ArmmanMidTechTitle();

            for (i = 0; i < midTechTitle.Length; i++)
            {
                cb.Items.Add(midTechTitle[i]);
            }
        }
    }
}
